package com.greyu.ysj.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtAutoExchange;
import com.greyu.ysj.entity.CbtAutoExchangeExample;
import com.greyu.ysj.entity.CbtDraw;
import com.greyu.ysj.entity.CbtDrawExample;
import com.greyu.ysj.mapper.CbtAutoExchangeMapper;
import com.greyu.ysj.mapper.CbtDrawMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtAutoExchangeService;

@Service
public class CbtAutoExchangeServiceImpl implements CbtAutoExchangeService{
	@Autowired
	private CbtAutoExchangeMapper cbtAutoExchangeMapper;
	@Override
	public ResultModel save(CbtAutoExchange cbtAutoExchange) {
		// TODO Auto-generated method stub
		try {	
			if ( cbtAutoExchange.getAccount()==null
					||cbtAutoExchange.getAmount()==null
					||cbtAutoExchange.getDrawDate()==null
					||cbtAutoExchange.getUserId()==null
	        		) {
	            return ResultModel.error(ResultStatus.DATA_NOT_NULL);
	        }
			//insert
	        this.cbtAutoExchangeMapper.insert(cbtAutoExchange);
		} catch (Exception e) {
			return ResultModel.error(ResultStatus.DRAW_INSERT_ERROR);
		}
	        return ResultModel.ok(cbtAutoExchange);
		
	}

	


	@Override
	public ResultModel updateCbtAutoExchange(Integer id, CbtAutoExchange cbtAutoExchange) {
		// TODO Auto-generated method stub
		CbtAutoExchange cbtAutoExchanges =this.cbtAutoExchangeMapper.selectByPrimaryKey(id);
		if(cbtAutoExchanges == null) {
			 return ResultModel.error(ResultStatus.DRAW_NOT_FOUND);
		}
		if(cbtAutoExchanges.getStatus() != null){
			cbtAutoExchanges.setStatus(cbtAutoExchange.getStatus());
		}
		 this.cbtAutoExchangeMapper.updateByPrimaryKey(cbtAutoExchange);
		return ResultModel.ok(cbtAutoExchange);
	}

	@Override
	public boolean isHashExist(String hash) {
		// TODO Auto-generated method stub
		CbtAutoExchangeExample cbtAutoExchangeExample = new CbtAutoExchangeExample();
		CbtAutoExchangeExample.Criteria criteria = cbtAutoExchangeExample.createCriteria();
		criteria.andDrawHashEqualTo(hash);
        List<CbtAutoExchange> list = this.cbtAutoExchangeMapper.selectByExample(cbtAutoExchangeExample);
        if(list==null||list.size()<1) {
        	return false;
        }else {
        	
        	return true;
        }
	}

	@Override
	public List<CbtAutoExchange> selectAll(CbtAutoExchange cbtAutoExchange) {
		// TODO Auto-generated method stub
		CbtAutoExchangeExample cbtAutoExchangeExample = new CbtAutoExchangeExample();
		CbtAutoExchangeExample.Criteria criteria = cbtAutoExchangeExample.createCriteria();
		criteria.andStatusEqualTo(cbtAutoExchange.getStatus());
        List<CbtAutoExchange> list = this.cbtAutoExchangeMapper.selectByExample(cbtAutoExchangeExample);
        return list;
	}

	@Override
	public int countTodayByUserId(Integer userId) {
		// TODO Auto-generated method stub
		int countTodayByUserId = this.cbtAutoExchangeMapper.countTodayByUserId(userId);
		return countTodayByUserId;
	}




	@Override
	public int selectMaxDrawId() {
		// TODO Auto-generated method stub
		int drawId=this.cbtAutoExchangeMapper.selectMaxDrawId();
		return drawId;
	}

}
